package com.example.musicgui.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class MusicClient {
    private Socket socket = null;

    public MusicClient() throws IOException {
        socket = new Socket(Client.HOST, Client.PORT_ONE);
    }

    public String start(String request) throws IOException {
        try {
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(outputStream, true);
            InputStream inputStream = socket.getInputStream();
            Scanner scannerNetwork = new Scanner(inputStream);
            writer.println(request);
            StringBuilder responseBuilder = new StringBuilder();
            String line = scannerNetwork.nextLine();
            responseBuilder.append(line);
            String result = responseBuilder.toString();
            return result;
        } finally {
            if (socket != null) {
                socket.close();
            }
        }
    }
}